App({
  onLaunch: function () {

    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    } else {
      wx.cloud.init({
        traceUser: true,
      })
    }
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
    
  },

  globalData: {
    userInfo: null,
    url:'https://m.zhcxkj.com/',
    hasLogin: false,
    token: null,
    uid: null,
    fid: null,
    email:null,
    pwd:null,
    cartItems:null,
    hasUserInfo: false
  },
  // lazy loading token

  getUserToken: function (callback) {
    var self = this

    self.globalData.cartItems = self.globalData.cartItems || wx.getStorageSync('cartItems') || null
    self.globalData.token = self.globalData.token || wx.getStorageSync('token') || null
    self.globalData.uid = self.globalData.uid || wx.getStorageSync('uid') || null
    self.globalData.fid = self.globalData.fid || wx.getStorageSync('fid') || null
    self.globalData.email = self.globalData.email || wx.getStorageSync('email') || null
    self.globalData.pwd = self.globalData.pwd || wx.getStorageSync('pwd') || null

    if (self.globalData.cartItems){
      let cartItems = self.globalData.cartItems.length;
        for (var i = 0; i < cartItems; i++) {
          delete self.globalData.cartItems[i].img_url;
          delete self.globalData.cartItems[i].name;
          delete self.globalData.cartItems[i].old_price;
          delete self.globalData.cartItems[i].price;
        }
    }

    if (self.globalData.token && self.globalData.uid) {
      callback(null, self.globalData.token)
    } else {
      wx.login({
        success: function (info) {
          wx.request({
            url: self.globalData.url +'index.php?_m=index/wx/token_openid',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              code: info.code,
              cart_json: JSON.stringify(self.globalData.cartItems)
            },
            success: function (resToken) {
              if (resToken.data.code == 0) {
                wx.removeStorageSync('cartItems')
                self.globalData.token = resToken.data.token
                wx.setStorageSync('token', self.globalData.token)
                self.globalData.uid = resToken.data.uid
                console.log(self.globalData.cartItems);
                callback(null, self.globalData.token)
              } else {
                wx.reLaunch({
                  url: '/pages/login/login'
                })
              }
            },
            fail: function (err) {
              console.log('拉取用户token失败，将无法正常使用开放接口等服务', err)
              callback(err)
            }
          })
        },
        fail: function (err) {
          console.log('wx.login 接口调用失败，将无法正常使用开放接口等服务', err)
          callback(err)
        }
      })
    }
  },

 
  
})
